import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../widgets/my_app_bar.dart';
import '../widgets/state_layout.dart';

abstract class BaseWidget extends StatefulWidget {
  const BaseWidget({Key? key}) : super(key: key);

  @override
  State<BaseWidget> createState() => getState();

  BaseWidgetState getState();
}

abstract class BaseWidgetState<T extends BaseWidget> extends State<T> {

  String _title = "";
  StateType _stateType = StateType.STATE_NORMAL;
  late StateLayout _stateLayout;

  showNoDataView(DoSomething doSomething){
    setState(() {
      this._stateType = StateType.STATE_NO_DATA;
      _stateLayout = StateLayout(StateType.STATE_NO_DATA,doSomething:doSomething,btText: "课程咨询",);
    });
  }

  showNoNetView(DoSomething doSomething){
    setState(() {
      this._stateType = StateType.STATE_NO_NET;
      _stateLayout = StateLayout(StateType.STATE_NO_NET,doSomething:doSomething,btText: "重试",);
    });
  }

  showNoLoginView(DoSomething doSomething){
    setState(() {
      this._stateType = StateType.STATE_NO_LOGIN;
      _stateLayout = StateLayout(StateType.STATE_NO_LOGIN,doSomething:doSomething,btText: "登录",);
    });
  }

  showContentView(){
    setState(() {
      this._stateType = StateType.STATE_NORMAL;
    });
  }

  @override
  void initState() {
    this._title = setPageTitle();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:_buildAppBar(),
      body: _buildBody(),
    );
  }

  MyAppBar? _buildAppBar() {
    return !_title.isEmpty?MyAppBar(
      title: _title,
      backgroundColor: Colors.white,
    ):null;
  }

  Widget _buildBody() {
    return Container(
      child: Stack(
        children: [
          buildContent(),
          if(_stateType != StateType.STATE_NORMAL) _stateLayout
        ],
      ),
    );
  }

  String setPageTitle();

  Widget buildContent();

}
